package com.jiezhuo.controller;

import com.jiezhuo.cache.RedisCache;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "工具controller")
@RequestMapping("/utils")
public class ControllerUtils {
    @Autowired
    private RedisCache redisCache;

    private final XxdwService xxdwService;
    private String[] tableList = {"dm_gjdq",
            "dm_jw_ckxz",
            "dm_jw_jxzzfs",
            "dm_jw_kcxz",
            "dm_jw_kssx",
            "dm_jw_kwwjzlb",
            "dm_jzg_bzxz",
            "dm_jzg_gwdj",
            "dm_jzg_gwzt",
            "dm_jzg_jszglb",
            "dm_jzg_rblx",
            "dm_jzg_ryxz",
            "dm_jzg_xj",
            "dm_jzg_ydlcztz",
            "dm_jzg_ydlx",
            "dm_jzg_ydzjhzz",
            "dm_jzg_zcdj",
            "dm_mz",
            "dm_nj",
            "dm_sf",
            "dm_sfzjlx",
            "dm_syzt",
            "dm_xbm",
            "dm_xd",
            "dm_xj_jtgx",
            "dm_xj_xsdqzt",
            "dm_xj_xslb",
            "dm_xk",
            "dm_xl",
            "dm_xs_glfs",
            "dm_xs_ydlcztz",
            "dm_xs_ydlx",
            "dm_xs_ydzjhzz",
            "dm_xx_xxbb",
            "dm_xx_xxlb",
            "dm_xzq",
            "dm_yw",
            "dm_zzmm"};


    @GetMapping("/getAllDistList")
    public R getAllDistList() {
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        Object allDistList = redisCache.getCacheObject("AllDistList");

        if (allDistList != null) {
            return R.ok().data("data", (Map<String, List<Map<String, Object>>>) allDistList);
        }


        for (int i = 0; i < tableList.length; i++) {
            List<Map<String, Object>> distList = xxdwService.getDistList(tableList[i]);
            map.put(tableList[i], distList);
        }
        redisCache.setCacheObject("AllDistList", map, 10, TimeUnit.MINUTES);
        return R.ok().data("data", map);
    }
//    @GetMapping("/getDistList")
//    public R getAllDistList() {
//        Map<String, List<Map<String, Object>>> map = new HashMap<>();
//        Object allDistList = redisCache.getCacheObject("AllDistList");
//
//        if (allDistList != null) {
//            return R.ok().data("data", (Map<String, List<Map<String, Object>>>) allDistList);
//        }
//
//
//        for (int i = 0; i < tableList.length; i++) {
//            List<Map<String, Object>> distList = xxdwService.getDistList(tableList[i]);
//            map.put(tableList[i], distList);
//        }
//        redisCache.setCacheObject("AllDistList", map, 10, TimeUnit.MINUTES);
//        return R.ok().data("data", map);
//    }
}
